//
//  PZRDetailTableViewCell.m
//  Moment
//
//  Created by pang on 15/12/23.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "PZRDetailTableViewCell.h"
#import "PZRDetailCollectionViewCell.h"
#import "PZRReadDetail.h"
@implementation PZRDetailTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self create];
    }
    return self;
}
-(void)create
{
    self.contentView.backgroundColor = [UIColor greenColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake([[UIScreen mainScreen]bounds].size.width/3-10,667/3*2/3-10);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.mainCollectionView  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width,  667/3*2+64) collectionViewLayout:flowLayout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    self.mainCollectionView.minimumZoomScale = 1;
    [self.mainCollectionView registerClass:[PZRDetailCollectionViewCell class] forCellWithReuseIdentifier:@"qqq"];
    [self.contentView addSubview:self.mainCollectionView];
    
    NSString *url = @"http://api2.pianke.me/read/columns";
    NSString *para = @"auth=CJFm1FH259KJDCsF1Gs3ikDJxQMejNrOMom1lsDzRlcWP22NJPSh5a7m34&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6";
    self.allArray = [NSMutableArray array];
    [GetData postUrl:url body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *bigArr = [bigDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in bigArr) {
            PZRReadDetail *detail = [[PZRReadDetail alloc]init];
            [detail setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:detail];
            //NSLog(@"%@",detail.coverimg);
        }
        [self.mainCollectionView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
    
    
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *chongyong = @"qqq";
    PZRDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:chongyong forIndexPath:indexPath];
    PZRReadDetail *detail = [[PZRReadDetail alloc]init];
    detail = [self.allArray objectAtIndex:indexPath.item];
    cell.aDetail = detail;
    NSLog(@"%@!!!",cell.aDetail.name);
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PZRReadDetail *detail = [[PZRReadDetail alloc]init];
    detail = [self.allArray objectAtIndex:indexPath.item];
    [self.ClickDelegate pushToClick:(detail.type)];
    
}
- (void)awakeFromNib
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}
@end
